package com.roamer.rest.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户信息过滤器
 *
 * @author roamer
 * @version V1.0
 * @date 2019-07-22 15:15
 */
@Slf4j
@WebFilter
@Order(1)
public class UserContextFilter extends OncePerRequestFilter {

    @Override
    public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        UserContextHolder.getUserContext().setCorrelationId(request.getHeader(Constants.CORRELATION_ID));
        UserContextHolder.getUserContext().setAuthToken(request.getHeader(Constants.AUTH_TOKEN));
        UserContextHolder.getUserContext().setUserId(request.getHeader(Constants.USER_ID));
        UserContextHolder.getUserContext().setOrgId(request.getHeader(Constants.ORG_ID));
        doFilter(request, response, filterChain);
    }
}
